package com.me.rabbitMQ.publish;


import com.me.rabbitMQ.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * 订阅模式的时候交换机必须先存在
 * 交换机如果不连接消息队列的话，发送的消息会丢失，因为它没有存储消息的能力
 */
public class Sender {
    private final static String EXCHANGE_NAME = "test_exchange";

    public static void main(String[] argv) throws Exception {
        // 获取到连接以及mq通道
        Connection connection = ConnectionUtil.getConnect();
        Channel channel = connection.createChannel();

        // 声明exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        // 消息内容
        String message = "Hello World!";
        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
        System.out.println(" Sent '" + message + "'");

        channel.close();
        connection.close();
    }
}
